package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import com.dudu.model.DataDictionary;

import core.CoreDao;
import core.db.DBConnectionUtil;

public class DataDictionaryDao {

	public static void main(String[] args) {

	
	}
	
	public int insert(CoreDao dao, DataDictionary bean) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_dictionary(type, data_key, data_value,comments )");
			sql.append("VALUES(?, ?, ?)");

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { 
						
						bean.getType(),
						bean.getData_key(),
						bean.getData_value(),
						bean.getComments()
					
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	

	public DataDictionary query(CoreDao dao, String type,String data_key) {

		Connection con = null;
		DataDictionary result = null;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");

			StringBuffer sql = new StringBuffer("select id,type,data_key,data_value,comments from t_dictionary " +
					"where type=? and data_key=?");
			
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { 
					type ,
					data_key
			});
			
			if (list != null && list.size() > 0) {
				List<DataDictionary> items = dao.covertToBean(list, DataDictionary.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
		
	}
}
